/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package wf3.core;

import acceso.aplicacion.Link;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Serializable;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import javax.servlet.ServletContext;

/**
 *
 * @author Rómulo Galindo Tanta
 */
public class Configuration implements Serializable {

    //
    boolean ms_login;
    //
    String ms_host;
    //
    boolean wf_filter;
    //
    String wf_filter_host;
    //
    String wf_modini;
    //
    boolean wf_reply;
    //
    String wf_reply_script;
    //
    String cookie_name;
    //
    String cookie_domain;

    //FILEPATH
    String files_path;

    //Indica el modo de la aplicación
    int co_modo;
    //Codigo interno de como funcionara la aplciación
    int co_system;
    //Nombre del sistema-> va en función al código de la aplicación
    String no_system;
    //
    String str_mst_ls_items;
    //Nombre del master de las aplicaciones
    String mst_no_nombre;
    //Lista de aplicaciones del grupo master
//    String mst_ls_items;
    List<Link> mst_ls_items;

    String cnt_titulo;
    String cnt_pag_width;
    String cnt_pag_t1_width;
    String cnt_pag_t2_width;

    //------------------------
    String log_formato;
    String log_level;
    //------------------------
    boolean servicio_correo;
    String smtp_host;
    String smtp_localhost;
    boolean servicio_twitter;
    String consumerKey;
    String consumerSecret;
    String twitterToken;
    String twitterTokenSecret;

    Properties properties;
    ServletContext sc;

    //--------- PARTE ESTATICA DE CONFIGURATOR
    List<Object> lsdepartamentos = new ArrayList<>();

    public Configuration(Properties properties, ServletContext servletContext) {
        this.properties = properties;
        sc = servletContext;
        build();
    }

    public void build() {
        //--------------------------------------
        ms_login = Boolean.parseBoolean(properties.getProperty("workflow.master_login"));
        ms_host = properties.getProperty("workflow.master_host");
        wf_filter = Boolean.parseBoolean(properties.getProperty("workflow.filter"));
        wf_filter_host = properties.getProperty("workflow.host");
        wf_modini = properties.getProperty("workflow.modini");
        wf_reply = Boolean.parseBoolean(properties.getProperty("workflow.replicacion"));
        wf_reply_script = properties.getProperty("workflow.replicacion_script");
        files_path = properties.getProperty("workflow.filespath");
        //******
        cookie_name = properties.getProperty("workflow.login.cookie.name");
        cookie_domain = properties.getProperty("workflow.master_login_cookie_domain");

        //--------------------------------------
        mst_no_nombre = properties.getProperty("app.master.no_nombre");
        str_mst_ls_items = properties.getProperty("app.master.ls_items");
        mst_ls_items = new Gson().fromJson(str_mst_ls_items, new TypeToken<List<Link>>() {
        }.getType());
        co_system = Integer.parseInt(properties.getProperty("app.co_system"));
        for (Link link : mst_ls_items) {
            if (link.getId().contentEquals("" + co_system)) {
                no_system = link.getName();
                break;
            }
        }
//        no_system = properties.getProperty("app.no_system");

        cnt_titulo = properties.getProperty("contenedor.titulo");
        cnt_pag_width = properties.getProperty("contenedor.pagina.width");
        cnt_pag_t1_width = properties.getProperty("contenedor.pagina.formulario.width_left");
        cnt_pag_t2_width = properties.getProperty("contenedor.pagina.formulario.width_right");

        //--------------------------------
        co_modo = Integer.parseInt(properties.getProperty("app.co_modo"));
        //------------------------------------------------
        log_formato = properties.getProperty("log.formato");
        log_level = properties.getProperty("log.level");
        //------------------------------------------------
        servicio_correo = Boolean.parseBoolean(properties.getProperty("workflow.servicio.correo"));
        smtp_host = properties.getProperty("mail.smtp.host");
        smtp_localhost = properties.getProperty("mail.smtp.localhost");

        servicio_twitter = Boolean.parseBoolean(properties.getProperty("workflow.servicio.twitter"));
        consumerKey = properties.getProperty("consumerKey");
        consumerSecret = properties.getProperty("consumerSecret");
        twitterToken = properties.getProperty("twitterToken");
        twitterTokenSecret = properties.getProperty("twitterTokenSecret");

        try {
            buildFile_mst_menu_p1(new File(sc.getResource("/workflow2/menu/mst_menu_p1.jsp").toURI()));
        } catch (IOException | URISyntaxException ep) {
            System.out.println("Ep:" + ep.getMessage());
        }

        //----------------------PARTE ESTATICA
        lsdepartamentos.add(new acceso.util.Departamento("041", "Amazonas"));
        lsdepartamentos.add(new acceso.util.Departamento("043", "Ancash"));
        lsdepartamentos.add(new acceso.util.Departamento("083", "Apurimac"));
        lsdepartamentos.add(new acceso.util.Departamento("054", "Arequipa"));
        lsdepartamentos.add(new acceso.util.Departamento("066", "Ayacucho"));
        lsdepartamentos.add(new acceso.util.Departamento("076", "Cajamarca"));
        lsdepartamentos.add(new acceso.util.Departamento("084", "Cusco"));
        lsdepartamentos.add(new acceso.util.Departamento("067", "Huancavelica"));
        lsdepartamentos.add(new acceso.util.Departamento("062", "Huanuco"));
        lsdepartamentos.add(new acceso.util.Departamento("056", "Ica"));
        lsdepartamentos.add(new acceso.util.Departamento("064", "Junin"));
        lsdepartamentos.add(new acceso.util.Departamento("044", "La Libertad"));
        lsdepartamentos.add(new acceso.util.Departamento("074", "Lambayeque"));
        lsdepartamentos.add(new acceso.util.Departamento("01", "Lima y Callao"));
        lsdepartamentos.add(new acceso.util.Departamento("065", "Loreto"));
        lsdepartamentos.add(new acceso.util.Departamento("082", "Madre de Dios"));
        lsdepartamentos.add(new acceso.util.Departamento("053", "Moquegua"));
        lsdepartamentos.add(new acceso.util.Departamento("063", "Pasco"));
        lsdepartamentos.add(new acceso.util.Departamento("073", "Piura"));
        lsdepartamentos.add(new acceso.util.Departamento("051", "Puno"));
        lsdepartamentos.add(new acceso.util.Departamento("042", "San Martin"));
        lsdepartamentos.add(new acceso.util.Departamento("052", "Tacna"));
        lsdepartamentos.add(new acceso.util.Departamento("072", "Tumbes"));
        lsdepartamentos.add(new acceso.util.Departamento("061", "Ucayali"));

    }

    public boolean isMs_login() {
        return ms_login;
    }

    public void setMs_login(boolean ms_login) {
        this.ms_login = ms_login;
    }

    public String getMs_host() {
        return ms_host;
    }

    public void setMs_host(String ms_host) {
        this.ms_host = ms_host;
    }

    public boolean isWf_filter() {
        return wf_filter;
    }

    public void setWf_filter(boolean wf_filter) {
        this.wf_filter = wf_filter;
    }

    public String getWf_filter_host() {
        return wf_filter_host;
    }

    public void setWf_filter_host(String wf_filter_host) {
        this.wf_filter_host = wf_filter_host;
    }

    public String getWf_modini() {
        return wf_modini;
    }

    public void setWf_modini(String wf_modini) {
        this.wf_modini = wf_modini;
    }

    public boolean isWf_reply() {
        return wf_reply;
    }

    public void setWf_reply(boolean wf_reply) {
        this.wf_reply = wf_reply;
    }

    public String getWf_reply_script() {
        return wf_reply_script;
    }

    public void setWf_reply_script(String wf_reply_script) {
        this.wf_reply_script = wf_reply_script;
    }

    public String getFiles_path() {
        return files_path;
    }

    public void setFiles_path(String files_path) {
        this.files_path = files_path;
    }

    public String getCookie_name() {
        return cookie_name;
    }

    public void setCookie_name(String cookie_name) {
        this.cookie_name = cookie_name;
    }

    public String getCookie_domain() {
        return cookie_domain;
    }

    public void setCookie_domain(String cookie_domain) {
        this.cookie_domain = cookie_domain;
    }

    public int getCo_modo() {
        return co_modo;
    }

    public void setCo_modo(int co_modo) {
        this.co_modo = co_modo;
    }

    public int getCo_system() {
        return co_system;
    }

    public void setCo_system(int co_system) {
        this.co_system = co_system;
    }

    public String getNo_system() {
        return no_system;
    }

    public void setNo_system(String no_system) {
        this.no_system = no_system;
    }

    public String getMst_no_nombre() {
        return mst_no_nombre;
    }

    public void setMst_no_nombre(String mst_no_nombre) {
        this.mst_no_nombre = mst_no_nombre;
    }

    public String getStr_mst_ls_items() {
        return str_mst_ls_items;
    }

    public void setStr_mst_ls_items(String str_mst_ls_items) {
        this.str_mst_ls_items = str_mst_ls_items;
    }

    public List<Link> getMst_ls_items() {
        return mst_ls_items;
    }

    public void setMst_ls_items(List<Link> mst_ls_items) {
        this.mst_ls_items = mst_ls_items;
    }

    //--------------------------------------------------------------------------------------------------
    public String getCnt_titulo() {
        return cnt_titulo;
    }

    public void setCnt_titulo(String cnt_titulo) {
        this.cnt_titulo = cnt_titulo;
    }

    public String getCnt_pag_width() {
        return cnt_pag_width;
    }

    public void setCnt_pag_width(String cnt_pag_width) {
        this.cnt_pag_width = cnt_pag_width;
    }

    public String getCnt_pag_t1_width() {
        return cnt_pag_t1_width;
    }

    public void setCnt_pag_t1_width(String cnt_pag_t1_width) {
        this.cnt_pag_t1_width = cnt_pag_t1_width;
    }

    public String getCnt_pag_t2_width() {
        return cnt_pag_t2_width;
    }

    public void setCnt_pag_t2_width(String cnt_pag_t2_width) {
        this.cnt_pag_t2_width = cnt_pag_t2_width;
    }

    //-------------------------------------------
    public String getLog_formato() {
        return log_formato;
    }

    public void setLog_formato(String log_formato) {
        this.log_formato = log_formato;
    }

    public String getLog_level() {
        return log_level;
    }

    public void setLog_level(String log_level) {
        this.log_level = log_level;
    }

    public boolean isServicio_correo() {
        return servicio_correo;
    }

    public void setServicio_correo(boolean servicio_correo) {
        this.servicio_correo = servicio_correo;
    }

    public String getSmtp_host() {
        return smtp_host;
    }

    public void setSmtp_host(String smtp_host) {
        this.smtp_host = smtp_host;
    }

    public String getSmtp_localhost() {
        return smtp_localhost;
    }

    public void setSmtp_localhost(String smtp_localhost) {
        this.smtp_localhost = smtp_localhost;
    }

    public boolean isServicio_twitter() {
        return servicio_twitter;
    }

    public void setServicio_twitter(boolean servicio_twitter) {
        this.servicio_twitter = servicio_twitter;
    }

    public String getConsumerKey() {
        return consumerKey;
    }

    public void setConsumerKey(String consumerKey) {
        this.consumerKey = consumerKey;
    }

    public String getConsumerSecret() {
        return consumerSecret;
    }

    public void setConsumerSecret(String consumerSecret) {
        this.consumerSecret = consumerSecret;
    }

    public String getTwitterToken() {
        return twitterToken;
    }

    public void setTwitterToken(String twitterToken) {
        this.twitterToken = twitterToken;
    }

    public String getTwitterTokenSecret() {
        return twitterTokenSecret;
    }

    public void setTwitterTokenSecret(String twitterTokenSecret) {
        this.twitterTokenSecret = twitterTokenSecret;
    }

    public List<Object> getLsdepartamentos() {
        return lsdepartamentos;
    }

    public void setLsdepartamentos(List<Object> lsdepartamentos) {
        this.lsdepartamentos = lsdepartamentos;
    }

    //---------------------------------------------------------------------------
    public Properties getProperties() {
        return properties;
    }

    public void setProperties(Properties properties) {
        this.properties = properties;
        build();
    }

    public void buildFile_mst_menu_p1(File file) throws IOException {
        try (PrintWriter printer = new PrintWriter(new FileWriter(file))) {
//            printer.print("<ul style=\"margin: 0px;\">");
            printer.print("<ul>");
            printer.print("<li>");
//            printer.print("<span style=\"display: block; height: 20px; padding: 5px 10px 0px 0px; vertical-align: bottom; color:#808080;\">" + mst_no_nombre + "&nbsp;&nbsp;&nbsp;|</span>");
            printer.print("<span >" + mst_no_nombre + "&nbsp;&nbsp;&nbsp;|</span>");
            printer.print("</li>");
            for (Link link : mst_ls_items) {
                printer.print("<li>");
                printer.print("<a " + (link.getId().contentEquals("" + co_system) ? "class=\"active\"" : "") + " href=\"" + link.getUrl() + "\" target=\"_blank\">" + link.getName() + "</a>");
                printer.print("</li>");
            }
            printer.print("</ul>");
        }
    }

}
